package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.mapper.UserBookMapper;
import com.example.demo.model.User;
import com.example.demo.model.UserBook;
import com.example.demo.service.AdminService;
import com.example.demo.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserBookMapper userBookMapper;



    @Override
    public boolean createRecord(UserBook record) {
        return userBookMapper.insert(record) > 0;
    }

    @Override
    public boolean updateRecord(UserBook record) {
        return userBookMapper.updateById(record) > 0;
    }

    @Override
    public boolean deleteRecord(Long recordId) {
        return userBookMapper.deleteById(recordId) > 0;
    }

    @Override
    public PageVo<UserBook> page(Long recordId, String title, User borrower, int pageNumber, int pageSize) {
        Page<UserBook> pageParam = new Page<>(pageNumber, pageSize);
        IPage<UserBook> userbookIPage = userBookMapper.selectUserBookByIdWithPaging(
                recordId,
                title,
                borrower != null ? borrower.getUsername() : null,
                pageParam
        );
        return new PageVo<>(userbookIPage);
    }

    //@Override
    //public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    //    User user = userBookMapper.findByUsername(username);
    //    if (user != null) {
    //        return new org.springframework.security.core.userdetails.User(user.getUsername(),
    //                user.getPassword(), true, true, true, true, AuthorityUtils.createAuthorityList("USER"));
    //    } else {
    //        throw new UsernameNotFoundException("用户名不存在");
    //    }
    //}
    @Override
    public Page<UserBook> getAllBorrowRecords(int page, int size) {
        Page<UserBook> userBookPage = new Page<>(page, size);
        return userBookMapper.selectPage(userBookPage, null);
    }
}